package basic

import (
	"encoding/json"
	"fmt"
	"io"
	"net"
	"os"
	"testgin0012/chat/client/process"
	"testgin0012/chat/common"
	"time"
)

type Client struct {
	Address string
	Network string
	Conn    *net.Conn
	User    *common.User
	SheUser *common.User
	Blocked bool
}

var (
	MyClient *Client
)

func NewClient(network string, address string) (c *Client, err error) {
	c = &Client{
		Address: address,
		Network: network,
		User:    &common.User{},
		SheUser: &common.User{},
	}
	cs, err := net.Dial(c.Network, c.Address)
	if err != nil {
		fmt.Printf("客户端创建连接失败\n")
		return
	}
	c.Conn = &cs
	return
}

func (client *Client) ShowMenu() {
	menu := `
------------客户信息管理系统-------
------------1.注册客户------------
------------2.登录客户------------
------------3.客户列表------------
------------4.一起群聊------------
------------5.简单私聊------------
------------6.测试接口------------
------------0.退出----------------
`
	data := ""
	if client.User.UserId > 0 {
		data = menu + fmt.Sprintf(`你好"%v-%v",`, client.User.NickName, client.User.UserId) + "请选择(1-9):"
	} else {
		data = "未登录, 请选择(1-9):"
		data = menu + data
	}
	fmt.Print(data)
}

func (client *Client) WaitSend() {
	time.Sleep(time.Millisecond * 2)
	for {
		client.ShowMenu()
		var choice int
		fmt.Scanln(&choice)
		fmt.Printf("\n你选择的是:%v\n", choice)
		userProcess := process.NewUserProcess()
		switch choice {
		case 0:
			userProcess.Exit(client.Conn)
		case 1:
			userProcess.Register(client.Conn)
		case 2:
			userProcess.Login(client.Conn, client.User)
		case 3:
			userProcess.List(client.Conn, client.User.UserId)
		case 4:
			userProcess.SayAll(client.Conn, client.User.UserId)
		case 5:
			userProcess.SayOne(client.Conn, client.User.UserId, &client.Blocked)
		case 6:
			//userProcess.Test(client.Conn, client.User.UserId)
			//process.NewUserProcess().SayOneStart(client.Conn, client.User.UserId, 11)
		}
	}
	fmt.Println("退出客户端")
}

func (client *Client) WaitReceived() {
	for {
		var serverData = make([]byte, 1024)
		i, err := (*client.Conn).Read(serverData)
		if err != nil {
			if err == io.EOF {
				os.Exit(0)
			} else {
				fmt.Printf("接收服务端返回失败, err=%v\n", err)
			}
			return
		}
		response := common.Response{}
		fmt.Printf("\n\n收到服务端的返回信息:%v\n", string(serverData[:i]))
		err = json.Unmarshal(serverData[:i], &response)
		if err != nil {
			fmt.Printf("Message外层json.Unmarshal解码失败, err=%v\n", err)
			return
		}
		handle := process.NewSmsProcess(&response)
		switch response.Code {
		case common.TypeResponseTest:
			handle.ResponseTest()
		case common.TypeResponseSayOneStart:
			handle.ResponseSayOneStart()
		case common.TypeResponseSayOne:
			handle.ResponseSayOne(client.User, client.SheUser, &client.Blocked)
			continue
		case common.TypeResponseSayAll:
			handle.ResponseSayAll()
		case common.TypeResponseUserList:
			handle.ResponseUserList()
		case common.TypeResponseLogin:
			handle.ResponseLogin(&client.User.UserId, &client.User.NickName)
		case common.TypeResponseString:
			handle.ResponseString()
		}
		client.ShowMenu()
	}
}
